/*
 * Name        : HttpResponse.java
 * Classes     : HttpResponse
 * Version     : 1.0
 * Date        : 2013-08-29
 *
 * Copyright 2013 CMCC.  All rights reserved.
 * This material, including documentation and any related computer
 * programs, is protected by copyright controlled by CMCC.  All
 * rights are reserved.  Copying, including reproducing, storing,
 * adapting or translating, any or all of this material requires the
 * prior written consent of CMCC.  This material also contains
 * confidential information which may not be disclosed to others
 * without the prior written consent of CMCC.
 */

package com.xgkj.diyiketang.http;





/**
 * Class Name  : HttpResponse
 * Author      : Zhang Lei
 * Date        : 13-9-3
 * Description :
 */
public class HttpResponse extends BaseResponse {

    private HttpEvent mHttpEvent;

    public HttpEvent getHttpEvent() {
        return mHttpEvent;
    }

    /**
     * to analyze the response data, and set them into result.
     * 
     * @param data, a HttpEvent Object that need to be analyzed.
     */
    @Override
    public void handleResponseData(Object data) {
        mResult = HttpResponseCode.HTTP_RESPONSE_FAIL_UNKNOWN_ERROR;//default value
        if (data != null && data instanceof HttpEvent) {
            mHttpEvent = (HttpEvent) data;
            switch (mHttpEvent.getType()) {
            case HTTP_EVENT_SUCCESS:
                mResult = HttpResponseCode.HTTP_RESPONSE_SUCCEED;
                break;
            case HTTP_EVENT_FAILED:
                mResult = checkErrorCode();
                break;
            default:
                break;
            }
        }
    }

    /**
     * called when the type of httpEvent is failed, to analyze the error code of
     * HttpEvent, and set them into result.
     * 
     * @return detail reason of http response failed.
     */
    private int checkErrorCode() {
        int result = HttpResponseCode.HTTP_RESPONSE_FAIL_UNKNOWN_ERROR;
        switch (mHttpEvent.getErrorCode()) {
        case HTTP_ERROR_TIMEOUT:
            result = HttpResponseCode.HTTP_RESPONSE_FAIL_TIMEOUT;
            break;
        case HTTP_ERROR_RESP_CODE_FAIL:
            result = HttpResponseCode.HTTP_RESPONSE_FAIL_STATUS_CODE;
            break;
        case HTTP_ERROR_CONNECT_FAIL:
            result = HttpResponseCode.HTTP_RESPONSE_FAIL_CONNECT;
        default:
            break;
        }
        return result;
    }
}
